﻿using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Text;

using Xyz.BusinessObjects;

namespace Xyz.DataObjects.Entity
{
    public class Mapper
    {
        internal static Administrator Map (AdministratorEntity obj)
        {
            return new Administrator
                       {
                           AdminId = obj.AdminId,
                           AdminLoginId = obj.AdminLoginId,
                           DateOfBirth = obj.DateOfBirth,
                           FirstName = obj.FirstName,
                           LastName = obj.LastName,
                           Gender = obj.Gender,
                           Password = obj.Password
                       };
        }

        internal static AdministratorEntity Map(Administrator obj)
        {
            return new AdministratorEntity
            {
                AdminId = obj.AdminId,
                AdminLoginId = obj.AdminLoginId,
                DateOfBirth = obj.DateOfBirth,
                FirstName = obj.FirstName,
                LastName = obj.LastName,
                Gender = obj.Gender,
                Password = obj.Password
            };
        }

        internal static Aircraft Map(AircraftEntity obj)
        {
            return new Aircraft
                       {
                           AircraftId = obj.AircraftId,
                           AircraftName = obj.AircraftName,
                           TotalSeat = obj.TotalSeat,
                           TotalCol = obj.TotalCol,
                           TotalRow = obj.TotalRow
                       };
        }

        internal static AircraftEntity Map(Aircraft obj)
        {
            return new AircraftEntity
            {
                AircraftId = obj.AircraftId,
                AircraftName = obj.AircraftName,
                TotalSeat = obj.TotalSeat,
                TotalCol = obj.TotalCol,
                TotalRow = obj.TotalRow
            };
        }

        internal static AircraftSeat Map(AircraftSeatEntity obj)
        {
            return new AircraftSeat
                       {
                           AircraftId = obj.AircraftId,
                           AircraftSeatId = obj.AircraftSeatId,
                           IsAisleSeat = obj.IsAisleSeat,
                           IsWindowSeat = obj.IsWindowSeat,
                           RewardPoint = obj.RewardPoint,
                           SeatColumn = obj.SeatColumn,
                           SeatNo = obj.SeatNo,
                           SeatRow = obj.SeatRow,
                           SeatTypeCode = obj.SeatTypeCode
                       };
        }

        internal static AircraftSeatEntity Map(AircraftSeat obj)
        {
            return new AircraftSeatEntity
            {
                AircraftId = obj.AircraftId,
                AircraftSeatId = obj.AircraftSeatId,
                IsAisleSeat = obj.IsAisleSeat,
                IsWindowSeat = obj.IsWindowSeat,
                RewardPoint = obj.RewardPoint,
                SeatColumn = obj.SeatColumn,
                SeatNo = obj.SeatNo,
                SeatRow = obj.SeatRow,
                SeatTypeCode = obj.SeatTypeCode
            };
        }

        internal static Airport Map(AirportEntity obj)
        {
            return new Airport
                       {
                           AirportCode = obj.AirportCode,
                           CityCode = obj.CityCode,
                           AirportName = obj.AirportName
                       };
        }

        internal static AirportEntity Map(Airport obj)
        {
            return new AirportEntity
            {
                AirportCode = obj.AirportCode,
                CityCode = obj.CityCode,
                AirportName = obj.AirportName
            };
        }

        internal static BillingAddress Map(BillingAddressEntity obj)
        {
            return new BillingAddress
                       {
                          AddressLine1 = obj.AddressLine1,
                          AddressLine2 = obj.AddressLine2,
                          AddressLine3 = obj.AddressLine3,
                          BillingAddressId = obj.BillingAddressId,
                          City = obj.City,
                          PostalCode = obj.PostalCode
                       };
        }

        internal static BillingAddressEntity Map(BillingAddress obj)
        {
            return new BillingAddressEntity
            {
                AddressLine1 = obj.AddressLine1,
                AddressLine2 = obj.AddressLine2,
                AddressLine3 = obj.AddressLine3,
                BillingAddressId = obj.BillingAddressId,
                City = obj.City,
                Country = obj.Country,
                PostalCode = obj.PostalCode
            };
        }

        internal static Booking Map(BookingEntity obj)
        {
            if (obj == null)
                return null;

            return new Booking
                       {
                           AdultNo = obj.AdultNo,
                           BookingCode = obj.BookingCode,
                           BookingId = obj.BookingId,
                           ChildNo = obj.ChildNo,
                           ContactEmailAddress = obj.ContactEmailAddress,
                           ContactName = obj.ContactName,
                           ContactNo = obj.ContactNo,
                           PaymentDue = obj.PaymentDue,
                           Status = obj.Status,
                           TotalTicketFare = obj.TotalTicketFare,
                           TripCode = obj.TripCode,
                           BookingDate = obj.BookingDate,
                           Tickets = obj.Tickets.Select(Map).ToList(),
                           CustomerId = obj.CustomerId
                       };
        }

        internal static BookingEntity Map(Booking obj)
        {
            var tickets = new EntityCollection<TicketEntity>();
            obj.Tickets.Select(Map).ToList().ForEach(tickets.Add);

            return new BookingEntity
            {
                AdultNo = obj.AdultNo,
                BookingId = obj.BookingId,
                BookingCode = obj.BookingCode,
                ChildNo = obj.ChildNo,
                ContactEmailAddress = obj.ContactEmailAddress,
                ContactName = obj.ContactName,
                ContactNo = obj.ContactNo,
                PaymentDue = obj.PaymentDue,
                Status = obj.Status,
                TotalTicketFare = obj.TotalTicketFare,
                TripCode = obj.TripCode,
                BookingDate = obj.BookingDate,
                Tickets = tickets,
                CustomerId = obj.CustomerId
            };
        }

        internal static CardPayment Map(CardPaymentEntity obj)
        {
            return new CardPayment
                       {
                           CardExpiryDate = obj.CardExpiryDate,
                           CardNo = obj.CardNo,
                           CardPaymentId = obj.CardPaymentId,
                           CardTypeCode = obj.CardTypeCode,
                           NameOnCard = obj.NameOnCard,
                           PaidAmount = obj.PaidAmount,
                           PaymentDate = obj.PaymentDate,
                           PaymentMethodCode = obj.PaymentMethodCode
                       };
        }

        internal static CardPaymentEntity Map(CardPayment obj)
        {
            return new CardPaymentEntity
            {
                BookingId = obj.BookingId,
                BillingAddresses = new EntityCollection<BillingAddressEntity>{Map(obj.BillingAddress)},
                CardExpiryDate = obj.CardExpiryDate,
                CardNo = obj.CardNo,
                CardPaymentId = obj.CardPaymentId,
                CardTypeCode = obj.CardTypeCode,
                NameOnCard = obj.NameOnCard,
                PaidAmount = obj.PaidAmount,
                PaymentDate = obj.PaymentDate,
                PaymentMethodCode = obj.PaymentMethodCode
            };
        }

        internal static CardType Map(CardTypeEntity obj)
        {
            return new CardType
                       {
                           CardTypeCode = obj.CardTypeCode,
                           CardTypeDescription = obj.CardTypeDescription
                       };
        }

        internal static CardTypeEntity Map(CardType obj)
        {
            return new CardTypeEntity
            {
                CardTypeCode = obj.CardTypeCode,
                CardTypeDescription = obj.CardTypeDescription
            };
        }

        internal static City Map(CityEntity obj)
        {
            return new City
                       {
                           CityCode = obj.CityCode,
                           CityName = obj.CityName,
                           CountryCode = obj.CountryCode
                       };
        }

        internal static CityEntity Map(City obj)
        {
            return new CityEntity
            {
                CityCode = obj.CityCode,
                CityName = obj.CityName,
                CountryCode = obj.CountryCode
            };
        }

        internal static Country Map(CountryEntity obj)
        {
            return new Country
                       {
                           CountryCode = obj.CountryCode,
                           CountryName = obj.CountryName
                       };
        }

        internal static CountryEntity Map(Country obj)
        {
            return new CountryEntity
            {
                CountryCode = obj.CountryCode,
                CountryName = obj.CountryName
            };
        }

        internal static Customer Map(CustomerEntity obj)
        {
            if (obj.GetType() == typeof(MemberEntity))
            {
                var entity = obj as MemberEntity;


                return new Member
                {
                    MemberId = entity.MemberId,
                    Address1 = entity.Address1,
                    Address2 = entity.Address2,
                    Address3 = entity.Address3,
                    City = entity.City,
                    Country = entity.Country,
                    CustomerId = entity.CustomerId,
                    DateOfBirth = entity.DateOfBirth,
                    FirstName = entity.FirstName,
                    Gender = entity.Gender,
                    LastName = entity.LastName,
                    MemberPoints = entity.MemberPoints,
                    PassportExpiryDate = entity.PassportExpiryDate,
                    PassportNo = entity.PassportNo,
                    Password = entity.Password,
                    RoleId = 2,
                    UserName = entity.UserName,
                    PostalCode = entity.PostalCode
                };

            }

            if (obj.GetType() == typeof(AgentEntity))
            {
                var entity = obj as AgentEntity;
                return new Agent
                {
                    AgentId = entity.AgentId,
                    CompanyName = entity.CompanyName,
                    CustomerId = entity.CustomerId,
                    DateOfBirth = entity.DateOfBirth,
                    DiscountRate = entity.DiscountRate,
                    FirstName = entity.FirstName,
                    LastName = entity.LastName,
                    Gender = entity.Gender,
                    Password = entity.Password,
                    RoleId = 1,
                    UserName = entity.UserName
                };
            }

            return null;
        }

        internal static CustomerEntity Map(Customer obj)
        {
            if (obj.GetType() == typeof(Member))
            {
                var entity = obj as Member;


                return new MemberEntity()
                {
                    MemberId = entity.MemberId,
                    Address1 = entity.Address1,
                    Address2 = entity.Address2,
                    Address3 = entity.Address3,
                    City = entity.City,
                    Country = entity.Country,
                    CustomerId = entity.CustomerId,
                    DateOfBirth = entity.DateOfBirth,
                    FirstName = entity.FirstName,
                    Gender = entity.Gender,
                    LastName = entity.LastName,
                    MemberPoints = entity.MemberPoints,
                    PassportExpiryDate = entity.PassportExpiryDate,
                    PassportNo = entity.PassportNo,
                    Password = entity.Password,
                    RoleId = 2,
                    UserName = entity.UserName,
                    PostalCode = entity.PostalCode
                };

            }

            if (obj.GetType() == typeof(Agent))
            {
                var entity = obj as Agent;
                return new AgentEntity
                {
                    AgentId = entity.AgentId,
                    CompanyName = entity.CompanyName,
                    CustomerId = entity.CustomerId,
                    DateOfBirth = entity.DateOfBirth,
                    DiscountRate = entity.DiscountRate,
                    FirstName = entity.FirstName,
                    LastName = entity.LastName,
                    Gender = entity.Gender,
                    Password = entity.Password,
                    RoleId = 1,
                    UserName = entity.UserName
                };
            }

            return null;
        }

        internal static Flight Map(FlightEntity obj)
        {
            return new Flight
                       {
                           ArrivalAirport = obj.ArrivalAirport,
                           ArrivalDateTime = obj.ArrivalDateTime,
                           ArrivalTerminal = obj.ArrivalTerminal,
                           DepartureAirport = obj.DepartureAirport,
                           DepartureDateTime = obj.DepartureDateTime,
                           DepartureTerminal = obj.DepartureTerminal,
                           Distance = obj.Distance,
                           FlightCode = obj.FlightCode,
                           FlightId = obj.FlightId,
                           FlightStatus = obj.FlightStatus,
                           TaxSurcharge = obj.TaxSurcharge,
                           AircraftId = obj.AircraftId
                       };
        }

        internal static FlightEntity Map(Flight obj)
        {
            return new FlightEntity
            {
                ArrivalAirport = obj.ArrivalAirport,
                ArrivalDateTime = obj.ArrivalDateTime,
                ArrivalTerminal = obj.ArrivalTerminal,
                DepartureAirport = obj.DepartureAirport,
                DepartureDateTime = obj.DepartureDateTime,
                DepartureTerminal = obj.DepartureTerminal,
                Distance = obj.Distance,
                FlightCode = obj.FlightCode,
                FlightId = obj.FlightId,
                FlightStatus = obj.FlightStatus,
                TaxSurcharge = obj.TaxSurcharge,
                AircraftId = obj.AircraftId
            };
        }

        internal static FlightPrice Map(FlightPriceEntity obj)
        {
            return new FlightPrice
                       {
                           Price = obj.Price,
                           SeatTypeCode = obj.SeatTypeCode
                       };
        }

        internal static FlightPriceEntity Map(FlightPrice obj)
        {
            return new FlightPriceEntity
            {
                Price = obj.Price,
                SeatTypeCode = obj.SeatTypeCode
            };
        }

        internal static FlightSeat Map(FlightSeatEntity obj)
        {
            return new FlightSeat
                       {
                           FlightSeatId = obj.FlightSeatId,
                           Status = obj.Status,
                           AircraftSeatId = obj.AircraftSeatId,
                           FlightId = obj.FlightId
                           //AircraftSeat = Map(obj.AircraftSeat)
                       };
        }

        internal static FlightSeatEntity Map(FlightSeat obj)
        {
            return new FlightSeatEntity
            {
                FlightSeatId = obj.FlightSeatId,
                Status = obj.Status,
                AircraftSeatId = obj.AircraftSeatId,
                FlightId = obj.FlightId
                //AircraftSeat = Map(obj.AircraftSeat)
            };
        }

        internal static Gst Map(GstEntity obj)
        {
            return new Gst
                       {
                           EffectiveDate = obj.EffectiveDate,
                           Percentage = obj.Percentage
                       };
        }

        internal static GstEntity Map(Gst obj)
        {
            return new GstEntity
            {
                EffectiveDate = obj.EffectiveDate,
                Percentage = obj.Percentage
            };
        }

        internal static MemberPayment Map(MemberPaymentEntity obj)
        {
            return new MemberPayment
                       {
                           BookingId = obj.BookingId,
                           MemberId = obj.MemberId,
                           PaymentDate = obj.PaymentDate,
                           PaymentPoints = obj.PaymentPoints
                       };
        }

        internal static MemberPaymentEntity Map(MemberPayment obj)
        {
            return new MemberPaymentEntity
            {
                BookingId = obj.BookingId,
                MemberId = obj.MemberId,
                PaymentDate = obj.PaymentDate,
                PaymentPoints = obj.PaymentPoints
            };
        }

        internal static Passenger Map(PassengerEntity obj)
        {
            return new Passenger
                       {
                           ContactNo = obj.ContactNo,
                           DateOfBirth = obj.DateOfBirth,
                           EmailAddress = obj.EmailAddress,
                           FirstName = obj.FirstName,
                           LastName = obj.LastName,
                           PassengerId = obj.PassengerId,
                           PassportExpiryDate = obj.PassportExpiryDate,
                           PassportNo = obj.PassportNo,
                           TitleCode = obj.TitleCode
                       };
        }

        internal static PassengerEntity Map(Passenger obj)
        {
            return new PassengerEntity
            {
                ContactNo = obj.ContactNo,
                DateOfBirth = obj.DateOfBirth,
                EmailAddress = obj.EmailAddress,
                FirstName = obj.FirstName,
                LastName = obj.LastName,
                PassengerId = obj.PassengerId,
                PassportExpiryDate = obj.PassportExpiryDate,
                PassportNo = obj.PassportNo,
                TitleCode = obj.TitleCode
            };
        }

        internal static PaymentMethod Map(PaymentMethodEntity obj)
        {
            return new PaymentMethod
                       {
                           PaymentMethodCode = obj.PaymentMethodCode,
                           PaymentMethodDescription = obj.PaymentMethodDescription
                       };
        }

        internal static PaymentMethodEntity Map(PaymentMethod obj)
        {
            return new PaymentMethodEntity
            {
                PaymentMethodCode = obj.PaymentMethodCode,
                PaymentMethodDescription = obj.PaymentMethodDescription
            };
        }

        internal static SeatType Map(SeatTypeEntity obj)
        {
            return new SeatType
                       {
                           SeatTypeCode = obj.SeatTypeCode,
                           SeatTypeDescription = obj.SeatTypeDescription,
                           SeatForSelection = obj.SeatForSelection
                       };
        }

        internal static SeatTypeEntity Map(SeatType obj)
        {
            return new SeatTypeEntity
            {
                SeatTypeCode = obj.SeatTypeCode,
                SeatTypeDescription = obj.SeatTypeDescription,
                SeatForSelection = obj.SeatForSelection
            };
        }

        internal static Service Map(ServiceEntity obj)
        {
            return new Service
                       {
                           Price = obj.Price,
                           ServiceCode = obj.ServiceCode,
                           ServiceDescription = obj.ServiceDescription,
                           ServiceCategoryCode = obj.ServiceCategoryCode
                       };
        }

        internal static ServiceEntity Map(Service obj)
        {
            return new ServiceEntity
            {
                Price = obj.Price,
                ServiceCode = obj.ServiceCode,
                ServiceDescription = obj.ServiceDescription,
                ServiceCategoryCode = obj.ServiceCategoryCode
            };
        }

        internal static PassengerService Map(PassengerServiceEntity obj)
        {
            return new PassengerService
            {
                TicketId = obj.TicketId,
                ServiceCode = obj.ServiceCode
            };
        }

        internal static PassengerServiceEntity Map(PassengerService obj)
        {
            return new PassengerServiceEntity
            {
                TicketId = obj.TicketId,
                ServiceCode = obj.ServiceCode
            };
        }

        internal static ServiceCategory Map(ServiceCategoryEntity obj)
        {
            return new ServiceCategory
                       {
                           ServiceCategoryCode = obj.ServiceCategoryCode,
                           ServiceCategoryDescription = obj.ServiceCategoryDescription
                       };
        }

        internal static ServiceCategoryEntity Map(ServiceCategory obj)
        {
            return new ServiceCategoryEntity
            {
                ServiceCategoryCode = obj.ServiceCategoryCode,
                ServiceCategoryDescription = obj.ServiceCategoryDescription
            };
        }

        internal static Terminal Map(TerminalEntity obj)
        {
            return new Terminal
                       {
                           TerminalCode = obj.TerminalCode,
                           TerminalDescription = obj.TerminalDescription
                       };
        }

        internal static TerminalEntity Map(Terminal obj)
        {
            return new TerminalEntity
            {
                TerminalCode = obj.TerminalCode,
                TerminalDescription = obj.TerminalDescription
            };
        }

        internal static Ticket Map(TicketEntity obj)
        {
            return new Ticket
                       {
                           TicketId = obj.TicketId,
                           TicketFare = obj.TicketFare,
                           BookingId = obj.BookingId,
                           Flight = Map(obj.Flight),
                           FlightSeat = Map(obj.FlightSeat),
                           Passenger = Map(obj.Passenger),
                           Services = obj.Services.Select(Map).ToList(),
                           FlightId = obj.FlightId
                       };
        }

        internal static TicketEntity Map(Ticket obj)
        {
            var services = new EntityCollection<PassengerServiceEntity>();
            obj.Services.Select(Map).ToList().ForEach(services.Add);

            return new TicketEntity
            {
                TicketId = obj.TicketId,
                TicketFare = obj.TicketFare,
                BookingId = obj.BookingId,
                //Flight = Map(obj.Flight),
                FlightId = obj.FlightId,
                FlightSeat = Map(obj.FlightSeat),
                Passenger = Map(obj.Passenger),
                Services = services
            };
        }

        internal static Trip Map(TripEntity obj)
        {
            return new Trip
                       {
                           TripCode = obj.TripCode,
                           TripDescription = obj.TripDescription
                       };
        }

        internal static TripEntity Map(Trip obj)
        {
            return new TripEntity
            {
                TripCode = obj.TripCode,
                TripDescription = obj.TripDescription
            };
        }

        internal static Title Map(TitleEntity obj)
        {
            return new Title
            {
                TitleCode = obj.TitleCode,
                TitleDescription = obj.TitleDescription
            }
            ;
        }

        internal static TitleEntity Map(Title obj)
        {
            return new TitleEntity
            {
                TitleCode = obj.TitleCode,
                TitleDescription = obj.TitleDescription
            }
            ;
        }
    }
}
